package com.example.dynamicdatasource.service.impl;

import com.example.dynamicdatasource.aop.annotation.DataSource;
import com.example.dynamicdatasource.entity.master.Employee;
import com.example.dynamicdatasource.enums.DataSourceType;
import com.example.dynamicdatasource.mapper.cluster.ClusterEmployeeMapper;
import com.example.dynamicdatasource.mapper.master.MasterEmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.example.dynamicdatasource.service.EmployeeService;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

/**
 * @author haitao
 * @date 2022/6/7 2:58 下午
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    @Qualifier("masterTransactionTemplate")
    private TransactionTemplate masterTransactionTemplate;

    @Autowired
    @Qualifier("clusterTransactionTemplate")
    private TransactionTemplate clusterTransactionTemplate;

    @Resource
    private MasterEmployeeMapper masterEmployeeMapper;

    @Resource
    private ClusterEmployeeMapper clusterEmployeeMapper;


    @Override
//    @DataSource(DataSourceType.MASTER)
//    @Transactional(rollbackFor = Exception.class,transactionManager = "masterTransactionManager")
    public void saveToMasterDatabase() {
        masterTransactionTemplate.execute(status -> {
            masterEmployeeMapper.saveAll(buildMockMasterEmployeeList());
            return true;
        });
    }

    @Override
//    @DataSource(DataSourceType.CLUSTER)
//    @Transactional(rollbackFor = Exception.class,transactionManager = "clusterTransactionManager")
    public void saveToClusterDatabase() {
        clusterTransactionTemplate.execute(status -> {
            clusterEmployeeMapper.saveAll(buildMockClusterEmployeeList());
            return true;
        });
    }

    @Override
    public void saveAll() {
        // 主
        masterTransactionTemplate.execute(masterStatus -> {
            masterEmployeeMapper.saveAll(buildMockMasterEmployeeList());
            // 从
            clusterTransactionTemplate.execute(clusterStatus -> {
                clusterEmployeeMapper.saveAll(buildMockClusterEmployeeList());
//                int i = 1/0;
                return true;
            });
            return true;
        });
    }

    private List<Employee> buildMockMasterEmployeeList() {
        List<Employee> employeeList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            Employee employee = Employee.builder().age(24).email("1210152726@qq.com").gender("男").lastName("海涛").build();
            employeeList.add(employee);
        }
        return employeeList;
    }

    private List<com.example.dynamicdatasource.entity.cluster.Employee> buildMockClusterEmployeeList() {
        List<com.example.dynamicdatasource.entity.cluster.Employee> employeeList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            com.example.dynamicdatasource.entity.cluster.Employee employee =
                    com.example.dynamicdatasource.entity.cluster.Employee.builder().age(24).email("1210152726@qq.com").gender("男").lastName("海涛").build();
            employeeList.add(employee);
        }
        return employeeList;
    }


}
